package com.project.controller;

import com.project.bean.UserBean;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @RequestMapping("/speak")
    public String speak(){

        return "你好，中国";
    }

    @RequestMapping("add")
    public String add(UserBean user, @RequestParam("name") String userName,
                      HttpServletRequest req, HttpServletResponse resp){
        System.out.println(user);
        System.out.println(userName);
        return "添加成功";
    }

    @RequestMapping(value = "getUser",method = RequestMethod.POST)
    public UserBean getUser(){
        return new UserBean(3,"吴六","女",5000, LocalDate.parse("2001-02-03"));
    }

    @RequestMapping(value = "del/{id}")
    public String del(@PathVariable("id") Integer userId){
        return "删除id="+userId+"的用户";
    }

    @RequestMapping("getList")
    public List<UserBean> getList(){
        List<UserBean> list = new ArrayList<>();
        list.add(new UserBean(3,"张六","女",5000, LocalDate.parse("2001-02-03")));
        list.add(new UserBean(4,"何勇","男",8000, LocalDate.parse("1998-08-16")));
        list.add(new UserBean(8,"朱坤","女",6000, LocalDate.parse("2000-12-09")));
        return list;
    }

    @RequestMapping("cut")
    public String cutPage(@RequestParam(value = "pageNO",defaultValue = "1") Integer pageNO){
        return "访问第"+pageNO+"页";
    }
}
